package cn.maihe.elg.operation.web.business;

import cn.maihe.elg.operation.model.enums.UserType;
import cn.maihe.elg.operation.model.vo.business.ExchangeUserVO;
import cn.maihe.elg.operation.model.vo.business.InstitutionUserVO;
import cn.maihe.elg.operation.model.vo.business.OrgRelationVO;
import cn.maihe.elg.operation.repository.entity.OrganizationDO;
import cn.maihe.elg.operation.repository.entity.entityCustom.UserDOCustom;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.business.ExchangeCenterService;
import cn.maihe.elg.operation.service.business.InstitutionService;
import cn.maihe.elg.operation.service.system.OrganizationService;
import cn.maihe.elg.operation.service.system.UserService;
import cn.maihe.elg.operation.utils.UserContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author caixh
 * @Date 2021/4/7.
 */
@Api(value = "交易用户管理", tags = "交易用户管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class UserRelationController {
    @Resource
    private ExchangeCenterService exchangeCenterService;
    @Resource
    private InstitutionService institutionService;
    @Resource
    private UserService userService;
    @Resource
    private OrganizationService organizationService;


    @ApiOperation(value = "获取机构关联关系接口", notes = "根据机构管理员id获取关联关系")
    @ApiImplicitParam(name = "userId", value = "管理员id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/orgRelation/{userId}", method = RequestMethod.GET)
    public Map getOrgRelation(@PathVariable Long userId) {
        Map result = new HashMap();
        List<ExchangeUserVO> exchangeUserVOS = exchangeCenterService.getOrgCenter(userId);
        result.put("center",exchangeUserVOS);
        List<InstitutionUserVO> institutionUserVOS = institutionService.getOrgInstitution(userId);
        result.put("institution",institutionUserVOS);

        return result;
    }

    @ApiOperation(value = "获取子账户关联关系接口", notes = "根据子账户id获取关联关系")
    @ApiImplicitParam(name = "userId", value = "管理员id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/accountRelation/{userId}", method = RequestMethod.GET)
    public Map getAccountRelation(@PathVariable Long userId) {
        UserContext uc = UserContextUtil.getUserContext();
        Long orgUserId = Long.parseLong(uc.getId());
        if(uc.getUserType()==UserType.ORG.getType()){

        }else if(uc.getUserType()==UserType.GENERAL.getType()){
            UserDOCustom user = userService.getUser(userId);
            if(user.getType()==UserType.ACCOUNT.getType()){
                Long orgId = user.getOrganizationId();
                OrganizationDO organizationDO = organizationService.getOrganization(orgId);
                orgUserId = organizationDO.getUserId();
            }else{
                //异常
            }

        }else {
            //异常
        }
        Map result = new HashMap();
        List<ExchangeUserVO> exchangeUserVOS = exchangeCenterService.getAccountCenter(userId,orgUserId);
        result.put("center",exchangeUserVOS);
        List<InstitutionUserVO> institutionUserVOS = institutionService.getAccountInstitution(userId,orgUserId);
        result.put("institution",institutionUserVOS);

        return result;
    }

    @ApiOperation(value = "保存机构关联关系接口", notes = "保存机构关联关系")
    @RequestMapping(value = "/orgRelation", method = RequestMethod.POST)
    public void saveRelation(@Validated @RequestBody OrgRelationVO orgRelationVO) {
        exchangeCenterService.saveOrgRelation(orgRelationVO);
    }

}
